package com.ruoyi.gams.controller;

import java.util.List;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gams.domain.GamsWindow;
import com.ruoyi.gams.service.IGamsWindowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 窗口Controller
 *
 * @author ruoyi
 * @date 2021-09-03
 */
@Api(tags = "广告系统后台：窗口管理")
@RestController
@RequestMapping("/gams/window")
public class GamsWindowController extends BaseController
{
    @Autowired
    private IGamsWindowService gamsWindowService;

    /**
     * 查询窗口列表
     */
    @PreAuthorize("@ss.hasPermi('gams:window:list')")
    @GetMapping("/list")
    public TableDataInfo list(GamsWindow gamsWindow)
    {
        startPage();
        List<GamsWindow> list = gamsWindowService.selectGamsWindowList(gamsWindow);
        return getDataTable(list);
    }

    /**
     * 导出窗口列表
     */
    @PreAuthorize("@ss.hasPermi('gams:window:export')")
    @Log(title = "窗口", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GamsWindow gamsWindow)
    {
        List<GamsWindow> list = gamsWindowService.selectGamsWindowList(gamsWindow);
        ExcelUtil<GamsWindow> util = new ExcelUtil<GamsWindow>(GamsWindow.class);
        return util.exportExcel(list, "窗口数据");
    }

    /**
     * 获取窗口详细信息
     */
    @PreAuthorize("@ss.hasPermi('gams:window:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(gamsWindowService.selectGamsWindowById(id));
    }

    /**
     * 新增窗口
     */
    @PreAuthorize("@ss.hasPermi('gams:window:add')")
    @Log(title = "窗口", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GamsWindow gamsWindow)
    {
        return toAjax(gamsWindowService.insertGamsWindow(gamsWindow));
    }

    /**
     * 修改窗口
     */
    @PreAuthorize("@ss.hasPermi('gams:window:edit')")
    @Log(title = "窗口", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GamsWindow gamsWindow)
    {
        return toAjax(gamsWindowService.updateGamsWindow(gamsWindow));
    }

    /**
     * 删除窗口
     */
    @PreAuthorize("@ss.hasPermi('gams:window:remove')")
    @Log(title = "窗口", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(gamsWindowService.deleteGamsWindowByIds(ids));
    }

    @ApiOperation("修改窗口屏ID")
    @PostMapping("/addWindow")
    public AjaxResult insertBatch(String windowName, Integer windowScreenId)
    {
        if (StringUtils.isEmpty(windowName))
        {
            throw new GlobalException("窗口名称不能为空");
        }
        GamsWindow firstByName = gamsWindowService.findFirstByName(windowName);
        if (firstByName == null)
        {
            throw new GlobalException("窗口名称错误");
        }
        firstByName.setWindowScreenId(windowScreenId);
        gamsWindowService.updateGamsWindow(firstByName);
        return AjaxResult.success();
    }
}
